home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Containers / ActiveXApp / CActiveXDocument.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  10.8 KB  |  446 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CActiveXDocument.cpp                    ©1996 Microsoft Corporation. All rights reserved.
  3. //                                            portions ©1995 Metrowerks Inc. All rights reserved.
  4. // =================================================================================
  5.  
  6. #include <LFile.h>
  7. #include <LPrintout.h>
  8. #include <LPlaceHolder.h>
  9. #include <LString.h>
  10. #include <LWindow.h>
  11. #include <PP_Messages.h>
  12. #include <UMemoryMgr.h>
  13. #include <UWindows.h>
  14.  
  15. #include "ActiveXAppConstants.h"
  16. #include "CActiveXView.h"
  17. #include "CActiveXDocument.h"
  18. #include "CActiveXApp.h"
  19.  
  20. CActiveXDocument *CActiveXDocument::sDefaultContainer = NULL;
  21.  
  22.  
  23. // ---------------------------------------------------------------------------------
  24. //        • CActiveXDocument::CContainer::Release
  25. // ---------------------------------------------------------------------------------
  26.  
  27. STDMETHODIMP_(ULONG)
  28. CActiveXDocument::Release(void)
  29. {
  30.     if (--mRefCount < 1)
  31.         DebugStr("\pToo many releases of CActiveXDocument::CContainer");
  32.  
  33.     return mRefCount;
  34. }
  35.  
  36.  
  37. // ---------------------------------------------------------------------------------
  38. //        • CActiveXDocument(LCommander*, FSSpec*)
  39. // ---------------------------------------------------------------------------------
  40.  
  41. CActiveXDocument::CActiveXDocument(
  42.     LCommander    *inSuper,
  43.     FSSpec        *inFileSpec )
  44.         : LSingleDoc( inSuper )
  45. {
  46.     // set up the default container so active x sites constructed can connect to it
  47.     sDefaultContainer = this;
  48.  
  49.     // Create window for our document.
  50.     mWindow = LWindow::CreateWindow( rPPob_ActiveXWindow, this );
  51.     Assert_( mWindow != nil );
  52.     mHostPort = ((GrafPort *)(mWindow->GetMacPort()));
  53.     mOwner = CActiveXApp::GetDefaultApplication();
  54.  
  55.     // Make activeX view target when window activated.
  56.     mActiveXView[0] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView );
  57.     if (kActiveXViewCount > 1)
  58.         mActiveXView[1] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView2 );
  59.     if (kActiveXViewCount > 2)
  60.         mActiveXView[2] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView3 );
  61.     if (kActiveXViewCount > 3)
  62.         mActiveXView[3] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView4 );
  63.     mWindow->SetLatentSub( mActiveXView[0] );
  64.  
  65.     mFocusView = NULL;
  66.     mModalFocusView = NULL;
  67.  
  68.     // Set name of window or open file.
  69.     if ( inFileSpec == nil ) {
  70.  
  71.         NameNewDoc();
  72.         
  73.     } else {
  74.     
  75.         OpenFile( *inFileSpec );
  76.     }
  77.     
  78.     // Make the window visible.
  79.     mWindow->Show();
  80. }
  81.  
  82.  
  83. // ---------------------------------------------------------------------------------
  84. //        • CActiveXDocument(LCommander*, FSSpec*)
  85. // ---------------------------------------------------------------------------------
  86.  
  87. CActiveXDocument::~CActiveXDocument(void)
  88. {
  89.     Int16    i;
  90.  
  91.     if (mFocusView)
  92.     {
  93.         mFocusView->SetFocus(ReleaseCommand, KeyboardFocus);
  94.         mFocusView->Release();
  95.         mFocusView = NULL;
  96.     }
  97.  
  98.     if (mModalFocusView)
  99.     {
  100.         mModalFocusView->SetFocus(ReleaseCommand, ModalFocus);
  101.         mModalFocusView->Release();
  102.         mModalFocusView = NULL;
  103.     }
  104.  
  105.     i = kActiveXViewCount;
  106.     while (i-- > 0)
  107.         if (mActiveXView[i])
  108.             delete mActiveXView[i];
  109.  
  110.     if (mOwner)
  111.         mOwner->RemoveContainer( this );
  112.  
  113.     if (mRefCount != 1)
  114.         DebugStr("\pToo few releases of CActiveXDocument::CContainer");
  115. }
  116.  
  117.  
  118. // ---------------------------------------------------------------------------------
  119. //        • NameNewDoc
  120. // ---------------------------------------------------------------------------------
  121.  
  122. void
  123. CActiveXDocument::NameNewDoc()
  124. {
  125.     // Setup the window title. Start with the default title.
  126.     LStr255    theTitle( "\pUntitled" );
  127.  
  128.     // Find the first available title. We could also check the window
  129.     // pane id if we wanted to make sure we didn't collide with other
  130.     // window types.
  131.     Int32    theNumber = 0;
  132.     while ( UWindows::FindNamedWindow( theTitle ) != nil ) {
  133.  
  134.         // An existing window has the current name
  135.         // Increment counter and try again.
  136.         ++theNumber;
  137.         theTitle = "\pUntitled ";
  138.         theTitle += (LStr255) theNumber;
  139.  
  140.     }        
  141.     
  142.     // Finally, set window title.
  143.     mWindow->SetDescriptor( theTitle );
  144. }
  145.  
  146.  
  147. // ---------------------------------------------------------------------------------
  148. //        • OpenFile
  149. // ---------------------------------------------------------------------------------
  150.  
  151. void
  152. CActiveXDocument::OpenFile(
  153.     FSSpec    &inFileSpec )
  154. {
  155.     Try_ {
  156.  
  157.         // Create a new file object.
  158.         mFile = new LFile( inFileSpec );
  159.         
  160.         // Open the data fork.
  161.         mFile->OpenDataFork( fsRdWrPerm );
  162.         
  163.         // Read the entire file and close the file.
  164. //        Handle    theTextH = mFile->ReadDataFork();
  165. //        mFile->CloseDataFork();
  166.         
  167.         // Put the contents in the text view
  168.         // and clear the dirty flag.
  169. //        mActiveXView->SetTextHandle( theTextH );
  170. //        mActiveXView->SetDirty( false );
  171.         
  172.         // Dispose of the text.
  173. //        ::DisposeHandle( theTextH );
  174.         
  175.         // Set window title to the name of the file.
  176.         mWindow->SetDescriptor( inFileSpec.name );
  177.  
  178.         // Flag that document has an associated file.
  179.         mIsSpecified = true;
  180.  
  181.     } Catch_( inErr ) {
  182.  
  183.         // Cleanup and rethrow the error.
  184.         delete this;
  185.         Throw_( inErr );
  186.     
  187.     } EndCatch_
  188. }
  189.  
  190.  
  191. // ---------------------------------------------------------------------------------
  192. //        • IsModified
  193. // ---------------------------------------------------------------------------------
  194.  
  195. Boolean
  196. CActiveXDocument::IsModified()
  197. {
  198.     short    i = kActiveXViewCount;
  199.  
  200.     while (i-- && !mIsModified)
  201.         mIsModified = mActiveXView[i]->IsDirty();
  202.  
  203.     return mIsModified;
  204. }
  205.  
  206.  
  207. // ---------------------------------------------------------------------------------
  208. //        • DoAESave
  209. // ---------------------------------------------------------------------------------
  210.  
  211. void
  212. CActiveXDocument::DoAESave(
  213.     FSSpec    &inFileSpec,
  214.     OSType    inFileType )
  215. {
  216.     // Delete the existing file object.
  217.     delete mFile;
  218.     
  219.     // Make a new file object.
  220.     mFile = new LFile( inFileSpec );
  221.     
  222.     // Get the proper file type.
  223.     OSType    theFileType = 'TEXT';
  224.     if ( inFileType != fileType_Default ) theFileType = inFileType;
  225.  
  226.     // Make new file on disk (we'll use
  227.     // SimpleText's creator for this example).
  228.     mFile->CreateNewDataFile( 'ttxt', theFileType );
  229.     
  230.     // Write out the data.
  231.     DoSave();
  232.  
  233.     // Change window title to reflect the new name.
  234.     mWindow->SetDescriptor( inFileSpec.name );
  235.  
  236.     // Document now has a specified file.
  237.     mIsSpecified = true;
  238. }
  239.  
  240.  
  241. // ---------------------------------------------------------------------------------
  242. //        • DoSave
  243. // ---------------------------------------------------------------------------------
  244.  
  245. void
  246. CActiveXDocument::DoSave()
  247. {
  248.     // Open the data fork.
  249.     mFile->OpenDataFork( fsRdWrPerm );
  250.  
  251.     // Get the text from the text view.
  252. //    Handle    theTextH = mActiveXView->GetTextHandle();
  253.     
  254.     // Lock the text handle.
  255. //    StHandleLocker    theLock( theTextH );
  256.     
  257.     // Write the text to the file.
  258. //    mFile->WriteDataFork( *theTextH, ::GetHandleSize( theTextH ) );
  259.  
  260.     // Close the data fork.
  261.     mFile->CloseDataFork();
  262.  
  263.     // Saving makes doc un-dirty.
  264.     short    i = kActiveXViewCount;
  265.  
  266.     while (i-- && !mIsModified)
  267.         mActiveXView[i]->SetDirty( false );
  268. }
  269.  
  270.  
  271. // ---------------------------------------------------------------------------------
  272. //        • DoRevert
  273. // ---------------------------------------------------------------------------------
  274.  
  275. void
  276. CActiveXDocument::DoRevert()
  277. {
  278.     // Open the data fork.
  279.     mFile->OpenDataFork( fsRdWrPerm );
  280.  
  281.     // Read the entire file contents and close the file.
  282. //    Handle    theTextH = mFile->ReadDataFork();
  283. //    mFile->CloseDataFork();
  284.     
  285.     // Put the contents in the text view
  286.     // and clear the dirty flag.
  287. //    mActiveXView->SetTextHandle( theTextH );
  288. //    mActiveXView->SetDirty( false );
  289.     
  290.     // Dispose of the text.
  291. //    ::DisposeHandle( theTextH );
  292.  
  293.     // Refresh the text view.
  294.     short    i = kActiveXViewCount;
  295.  
  296.     while (i-- && !mIsModified)
  297.         mActiveXView[i]->Refresh();
  298. }
  299.  
  300.  
  301. // ---------------------------------------------------------------------------------
  302. //        • FindCommandStatus
  303. // ---------------------------------------------------------------------------------
  304.  
  305. void
  306. CActiveXDocument::FindCommandStatus(
  307.     CommandT    inCommand,
  308.     Boolean        &outEnabled,
  309.     Boolean        &outUsesMark,
  310.     Char16        &outMark,
  311.     Str255        outName )
  312. {
  313.     switch ( inCommand ) {
  314.     
  315.         case cmd_Print:
  316.         case cmd_PrintOne:
  317.         {
  318.             // Short circuit the printing commands
  319.             // since this example doesn't print.
  320.             outEnabled = false;
  321.         }
  322.         break;
  323.  
  324.         default:
  325.         {
  326.             // Call inherited.
  327.             LSingleDoc::FindCommandStatus( inCommand,
  328.                 outEnabled, outUsesMark, outMark, outName );
  329.         }
  330.         break;
  331.  
  332.     }
  333. }
  334.  
  335.  
  336. // ---------------------------------------------------------------------------------
  337. //        • RequestFocus
  338. // ---------------------------------------------------------------------------------
  339.  
  340. ErrorCode
  341. CActiveXDocument::RequestFocus ( CActiveXView* inActiveXView, Boolean inAcquire, FocusSet inFocus)
  342. {
  343.     ErrorCode        theResult = S_OK;
  344.     CActiveXView*    saveModal = mModalFocusView;
  345.     Boolean8        modalReleased = false;
  346.     
  347.     if (inActiveXView == NULL)
  348.         return E_FAIL;
  349.  
  350.     // Modal focus
  351.     if (inFocus & ModalFocus)
  352.     {
  353.         if (inAcquire)
  354.         {
  355.             // If noone has the focus, give it to the requestor
  356.             if (mModalFocusView == NULL)
  357.             {
  358.                 mModalFocusView = inActiveXView;
  359.                 inActiveXView->AddRef();
  360.             }
  361.             // Does someone else have the focus?
  362.             else if (mModalFocusView != inActiveXView)
  363.             {
  364.                 // Ask them if they'll give it up
  365.                 if (mModalFocusView->SetFocus(RequestReleaseCommand, ModalFocus) == S_OK)
  366.                 {
  367.                     mModalFocusView->Release();
  368.                     mModalFocusView = inActiveXView;
  369.                     inActiveXView->AddRef();
  370.                     modalReleased = true;
  371.                 }
  372.                 else    // They won't release it - fail the request
  373.                     theResult = E_FAIL;
  374.             }
  375.         }
  376.         else    // !inAcquire -- release
  377.         {
  378.             // If the requestor holds the focus, release it.
  379.             if ((mModalFocusView != NULL) && (mModalFocusView == inActiveXView))
  380.             {
  381.                 mModalFocusView->Release();
  382.                 mModalFocusView = NULL;
  383.             }
  384.         }
  385.     }
  386.  
  387.     // Keyboard focus
  388.     if ((theResult == S_OK) && (inFocus & KeyboardFocus))
  389.     {
  390.         if (inAcquire)
  391.         {
  392.             // If noone has the focus, give it to the requestor
  393.             if (mFocusView == NULL)
  394.             {
  395.                 mFocusView = inActiveXView;
  396.                 inActiveXView->AddRef();
  397.                 SwitchTarget(inActiveXView);
  398.             }
  399.             // Does someone else have the focus?
  400.             else if (mFocusView != inActiveXView)
  401.             {
  402.                 // Ask them if they'll give it up
  403.                 if (mFocusView->SetFocus(RequestReleaseCommand, KeyboardFocus) == S_OK)
  404.                 {
  405.                     mFocusView->Release();
  406.                     mFocusView = inActiveXView;
  407.                     inActiveXView->AddRef();
  408.                     SwitchTarget(inActiveXView);
  409.                 }
  410.                 else    // They won't release it - fail the request
  411.                     theResult = E_FAIL;
  412.             }
  413.         }
  414.         else    // !inAcquire -- release
  415.         {
  416.             // If the requestor holds the focus, release it.
  417.             if ((mFocusView != NULL) && (mFocusView == inActiveXView))
  418.             {
  419.                 mFocusView->Release();
  420.                 mFocusView = NULL;
  421.                 SwitchTarget(this);
  422.             }
  423.         }
  424.     }
  425.  
  426.     // If there was a problem and the modal focus was changed, attempt to restore it
  427.     // to the state when we started.    
  428.     if ((theResult != S_OK) && inAcquire && modalReleased)
  429.     {
  430.         if (saveModal->SetFocus(TakeNextCommand, ModalFocus) == S_OK)
  431.         {
  432.             mModalFocusView->Release();
  433.             mModalFocusView = saveModal;
  434.             saveModal->AddRef();
  435.         }
  436.         else
  437.         {
  438.             mModalFocusView->Release();
  439.             mModalFocusView = NULL;
  440.         }
  441.     }
  442.     
  443.     return theResult;
  444. }
  445.  
  446.